using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Xml;

namespace LDSAgenda
{
    public partial class frmConfigSettings : DevExpress.XtraEditors.XtraForm
    {
        ConfigurationSettings ConfigSettings = new ConfigurationSettings();

        public frmConfigSettings()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            ConfigSettings.DBPath = openDataBaseFile();
            txtDBPath.Text = ConfigSettings.DBPath;
            WriteXML(ConfigSettings.DBPath);
        }

        private string openDataBaseFile()
        {
            fdOpenDatabase.ShowDialog();

            return fdOpenDatabase.FileName;
        }

        private void frmConfigSettings_Load(object sender, EventArgs e)
        {

        }

        //TODO: Read and save settings from text/XML file.
        void WriteXML(string DBPath)
        {
            try
            {
                //pick whatever filename with .xml extension
                string filename = "ConfigSettings.xml";

                XmlDocument xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.Load(filename);
                }
                catch (System.IO.FileNotFoundException)
                {
                    //if file is not found, create a new xml file
                    XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                    xmlWriter.WriteStartElement("Root");
                    //If WriteProcessingInstruction is used as above,
                    //Do not use WriteEndElement() here
                    //xmlWriter.WriteEndElement();
                    //it will cause the &ltRoot></Root> to be &ltRoot />
                    xmlWriter.Close();
                    xmlDoc.Load(filename);
                }
                XmlNode root = xmlDoc.DocumentElement;
                XmlElement childNode = xmlDoc.CreateElement("Configuration");
                XmlElement childNode2 = xmlDoc.CreateElement("DataBase");
                XmlText textNode = xmlDoc.CreateTextNode(DBPath);
                //textNode.Value = "hello, world";

                root.AppendChild(childNode);
                childNode.AppendChild(childNode2);
                childNode2.SetAttribute("DB", "SQLite3");
                childNode2.AppendChild(textNode);

                //textNode.Value = "replacing hello world";
                xmlDoc.Save(filename);
            }
            catch (Exception ex)
            {
                WriteError(ex.ToString());
            }
        }

        void WriteError(string str)
        {
            MessageBox.Show(str);
        }
    }
}